﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace PRE.Login
{
    public partial class FrmLogin : DevExpress.XtraEditors.XtraForm
    {
        ServiceReference_Thanhvien.WS_ThanhvienSoapClient sr_tv = new ServiceReference_Thanhvien.WS_ThanhvienSoapClient();
        WS.BLL.EntitiesBLL.ThanhvienBLL _bll = new WS.BLL.EntitiesBLL.ThanhvienBLL();
        
        IList<ServiceReference_Thanhvien.Thanhvien> list;

        public FrmLogin()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if(txtUser.Text != "" && txtPass.Text != "")
            {
                ServiceReference_Thanhvien.Thanhvien tv = new ServiceReference_Thanhvien.Thanhvien();
                tv.taikhoan = txtUser.Text; tv.matkhau = _bll.EncodeMD5(txtPass.Text);

                list = (IList<ServiceReference_Thanhvien.Thanhvien>)sr_tv.CheckThanhvien(tv);

                if (list != null)
                {
                    DataTable dtb = WS.UTL.BaseUTL.IListToDataTable(list);
                    var sss = _bll.CheckLogin(dtb, txtUser.Text, txtPass.Text);
                    if (sss.Login)
                    {
                        FrmMain._sss = sss;
                        Close();
                    }
                    else
                        MessageBox.Show("Lỗi! Mật khẩu không đúng", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                    MessageBox.Show("Lỗi! Tài khoản hoặc mật khẩu không đúng", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
                MessageBox.Show("Lỗi! Vui lòng nhập đầy đủ thông tin", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {

        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}